package com.ledi.guns.modular.ledipay.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ledi.guns.common.persistence.dao.BillAlipayMapper;
import com.ledi.guns.common.persistence.dao.BillWeixinMapper;
import com.ledi.guns.common.persistence.model.BillAlipay;
import com.ledi.guns.common.persistence.model.BillWeixin;
import com.ledi.guns.core.base.controller.BaseController;
import com.ledi.guns.core.util.StringUtil;
import com.ledi.guns.modular.ledipay.service.impl.BillDetaileServiceImpl;
import com.ledi.guns.modular.ledipay.service.impl.EduPayAliObserver;
import com.ledi.guns.modular.ledipay.service.impl.EduPayWexinObserver;

/**
 * 文件名称： com.ledi.guns.modular.system.controller.TriggerInvokeController.java<br/>
 * 初始作者： 周登文<br/>
 * 创建日期： 2017年12月12日<br/>
 * 功能说明： 批处理任务 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (橘子股份-幼儿事业部) 2012-2017.All rights reserved.<br/>
 */
@RestController
@RequestMapping("/trigger")
public class TriggerInvokeController extends BaseController {

	private final static Logger	logger	= LoggerFactory.getLogger(TriggerInvokeController.class);
	@Autowired
	BillAlipayMapper			billAlipayMapper;
	@Autowired
	BillWeixinMapper			billWeixinMapper;
	@Autowired
	EduPayAliObserver			eduPayAliObserver;
	@Autowired
	EduPayWexinObserver			eduPayWexinObserver;
	@Autowired
	BillDetaileServiceImpl		billDetaileServiceImpl;

	@RequestMapping("/handle-expire-bill")
	public String handleExpireBill() {

		logger.info("开始处理支付宝未支付的账单");
		List<BillAlipay> list = billAlipayMapper.selectExpireBill();
		if (list != null) {
			logger.info("查询到" + list.size() + "条支付宝未支付的账单");
			for (BillAlipay billAlipay : list) {
				eduPayAliObserver.havePay(billAlipay, billAlipay.getSchoolPid(),
						billAlipay.getAlipayOrderNo());
			}
		}
		logger.info("开始处理微信未支付的账单");

		List<BillWeixin> billWeixinList = billWeixinMapper.selectScanBill();
		if (billWeixinList != null) {
			logger.info("查询到" + billWeixinList.size() + "条微信未支付的账单");
			for (BillWeixin billWeixin : billWeixinList) {
				if (StringUtil.isBlank(billWeixin.getSubMchId())) {
					continue;
				}
				eduPayWexinObserver.havePay(billWeixin, billWeixin.getBillDetailId(),
						billWeixin.getSubMchId());
			}
		}

		return "handle success";
	}
}
